package com.kehutong.contact.admin;

import com.kehutong.common.util.Token;
import com.kehutong.contact.entity.Contact;
import com.kehutong.contact.entity.ContactComment;
import com.kehutong.contact.entity.ContactDynamics;
import com.kehutong.contact.entity.Customer;
import com.kehutong.contact.enums.FollowUpType;
import com.kehutong.contact.service.ContactDynamicsService;
import com.kehutong.contact.service.CustomerService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;

import java.util.List;

/**
 * @author liujinwang
 * description: 联系人详情动态
 */
@MvcAction
@ReqMapping("/admin/crm/contact/dynamics")
public class ContactDynamicsController {

    static final Logger logger = LoggerFactory.getLogger(ContactDynamicsController.class);

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private ContactDynamicsService contactDynamicsService;

    @ReqMapping("/page")
    public Page<ContactDynamics> page(JSONObject jsonObject) throws Exception {
        return contactDynamicsService.page(jsonObject);
    }

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        return contactDynamicsService.list(jsonObject);
    }

    /**
     * 新建跟进动态
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Token token, JSONObject jsonObject) throws Exception{
        return contactDynamicsService.save(token,jsonObject);
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(ContactDynamics contactDynamics) throws Exception {
        return contactDynamicsService.update(contactDynamics);
    }

    @ReqMapping("/delete")
    public Object delete(ContactDynamics contactDynamics) throws Exception{
        return contactDynamicsService.delete(contactDynamics);
    }

    @ReqMapping("/get")
    public Object get(ContactDynamics contactDynamics) throws Exception{
        contactDynamics = Pool.get(ContactDynamics.class, contactDynamics.getId());
        List<ContactComment> commentList = jdbcSession.findArray(ContactComment.class)
                .eq("contactId", contactDynamics.getContactId())
                .eq("contactDynamicsId", contactDynamics.getId())
                .exe();
        contactDynamics.setCommentsCount((long) commentList.size());
        return Result.success(contactDynamics);
    }
}
